package com.zte.shopping.action.backend;

import com.zte.shopping.constant.ResponseCodeConstant;
import com.zte.shopping.entity.ProductType;
import com.zte.shopping.exception.ProductTypeExistException;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.service.ProductTypeService;
import com.zte.shopping.util.ExcelImpl;
import com.zte.shopping.util.PageSupport;
import com.zte.shopping.util.ResponseResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: ssm
 * @description: 后台产品类型管理界面
 * @author: zuti
 * @create: 2020-06-23 22:09
 **/
@Controller
@RequestMapping("productType")
public class BackendProductTypeManagerController {

    private  static  final Logger logger= Logger.getLogger(BackendProductTypeManagerController.class);
    @Autowired
    ProductTypeService productTypeService;


    @RequestMapping("/toProductType")
    public String   toProductType( @RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                  Model model){

        logger.debug("enter BackendProductTypeManagerController------> toProductType param: pageIndex"+pageIndex);
        List<ProductType> productTypeList = productTypeService.productTypeList(pageIndex);

        PageSupport pageSupport = new PageSupport();
        pageSupport.setTotalCount(this.productTypeService.productTypeListCount());
        pageSupport.setCurrentPageNo(pageIndex);

        model.addAttribute("pageSupport", pageSupport);
        model.addAttribute("productTypeList", productTypeList);


        return "backend/productTypeManager";
    }

    /**
     * @Author zuti
     * @Description //TODO zuti666
     * @Date 7:11 2020/6/29
     * @Param [id, status, pageNo]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/modifyProductTypeStatus")
    public ModelAndView modifyStatus(@RequestParam("id") String id,
                                     @RequestParam("status") String status,
                                     @RequestParam("pageNo") String pageNo)
    {
        ModelAndView  modelAndView = new ModelAndView();

        productTypeService.modifyStatus(id, status);

        modelAndView.setViewName("redirect:http://localhost:8080/productType/toProductType?pageIndex=" + pageNo);

        return modelAndView;
    }

    /**
     * @Author zuti
     * @Description //TODO 根据 商品类型ID 修改   商品类型信息
     * @Date 7:59 2020/6/29
     * @Param [id, name]
     * @return com.zte.shopping.util.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/modifyName")
    public ResponseResult modifyName(@RequestParam("id") String id,
                                     @RequestParam("name") String name)
    {
        ResponseResult  result = new ResponseResult();

        try
        {
            productTypeService.modifyName(id, name);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    /**
     * 添加商品类型(采用ajax来添加)
     *
     * 我们需要让SpringMVC知道,我们需要返回的是数据模型 ---> 采用@ResponseBoby注解标注在当前方法addType上
     *
     * @RequestMapping注解中的produces属性表示:设置响应体的格式
     * 即在此方法上加上  @RequestMapping(value = "/addType",produces="text/html;charset=utf-8") 为了解决此方法的响应数据乱码问题
     *
     */
    @ResponseBody
    @RequestMapping(value = "/addType")
    public ResponseResult addType(@RequestParam("name") String name)
    {
        ResponseResult result = new ResponseResult();
        try
        {
            productTypeService.addType(name);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

        } catch (Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        }
        return result;
    }

 /**
  * @Author zuti
  * @Description //TODO 导出excel
  * @Date 10:04 2020/6/29
  * @Param [req]
  * @return org.springframework.web.servlet.ModelAndView
  **/
 @RequestMapping("/exportExcel")
 public @ResponseBody String ExportExcel(  @RequestParam(value = "pageIndex",required = false,defaultValue = "0") Integer pageIndex,
                                HttpServletResponse response){

     ExcelImpl excelImpl = new ExcelImpl();
     response.setContentType("application/binary;charset=UTF-8");

     List<ProductType> typeList = new ArrayList<>();
     if(pageIndex==0){
         typeList=productTypeService.allProductTypeList();
     }else{
         typeList=productTypeService.productTypeList(pageIndex);
     }


     try{
         ServletOutputStream out=response.getOutputStream();
         try {
             //设置文件头：最后一个参数是设置下载文件名(这里我们叫：张三.pdf)
             response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("商品类型.xls", "UTF-8"));
         } catch (UnsupportedEncodingException e1) {
             e1.printStackTrace();
         }

         String[] tips = {"编号", "类型名称", "状态"};
         excelImpl.export(tips,out,typeList);
         return "success";
     } catch(Exception e){
         e.printStackTrace();
         return "导出信息失败";
     }
 }


}
